<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" ref="filters" :size="size">
        <el-form-item prop="shopname">
          <el-input
            v-model="filters.shopname"
            placeholder="门店名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="businessnumber">
          <el-input
            v-model="filters.businessnumber"
            placeholder="商家编号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="shopstatus">
          <el-select v-model="filters.shopstatus" placeholder="请选择商铺状态">
            <el-option
              v-for="item in shopstatus"
              :key="item.key"
              :label="item.value"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-search"
            :label="$t('action.search')"
            type="primary"
            @click="findPage(null)"
          />
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-refresh" :label="$t('action.reset')"
            type="primary"
            @click="resetForm('filters')"
          />
        </el-form-item>
        <el-form-item>
          <kt-button label="状态审核" type="primary" @click="setStaus()" />
        </el-form-item>
      </el-form>
    </div>
    <div
      class="toolbar"
      style="float:right;padding-top:10px;padding-right:15px;"
    >
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button
                icon="fa fa-refresh"
                @click="findPage(null)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button
                icon="fa fa-filter"
                @click="displayFilterColumnsDialog"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button
                icon="fa fa-file-excel-o"
                @click="downloadExcel"
                id="downloadExcel"
              ></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="false"
      :showBatchDelete="false"
      :showDeleteOperation="false"
      @findPage="findPage"
      @handleDetail="handleDetail"
      :showDetailOperation="true"
      @selectionChange="selectionChange"
      ref="CyTable"
    ></cy-table>

    <!--新增编辑界面-->
    <el-dialog
      title="详情"
      width="80%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        :inline="true"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input
            v-model="dataForm.id"
            :disabled="true"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="dataForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="dataForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="提现比例" prop="withd_proport">
          <el-input
            v-model="dataForm.withd_proport"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="LBS服务范围(公里)" prop="service_range">
          <el-input
            v-model="dataForm.service_range"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺类别" prop="type">
          <el-select v-model="dataForm.type" style="width: 214.5px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in type"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营业执照编号" prop="license_number">
          <el-input
            v-model="dataForm.license_number"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="法人姓名" prop="legal_name">
          <el-input
            v-model="dataForm.legal_name"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="法人身份证" prop="legal_number">
          <el-input
            v-model="dataForm.legal_number"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="注册资本(万)" prop="register_capital">
          <el-input
            v-model="dataForm.register_capital"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="经营范围" prop="scope">
          <el-input v-model="dataForm.scope" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="对公账户名称" prop="account_name">
          <el-input
            v-model="dataForm.account_name"
            auto-complete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="对公账户账号" prop="account_number">
          <el-input
            v-model="dataForm.account_number"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="账号开户地址" prop="account_address">
          <el-input
            v-model="dataForm.account_address"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" style="width: 214.5px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in list"
              :key="item.key"
              :label="item.val"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保证金" prop="bail">
          <el-input v-model="dataForm.bail" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="是否缴纳" prop="bail_flag">
          <el-select v-model="dataForm.bail_flag" style="width: 214.5px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in bail_flag"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营业时间" prop="opentime">
          <el-input v-model="dataForm.opentime" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="dataForm.latitude" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="dataForm.longitude" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商铺联系电话" prop="mobile">
          <el-input v-model="dataForm.mobile" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact_person">
          <el-input
            v-model="dataForm.contact_person"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺模板地址" prop="shop_address">
          <el-input
            v-model="dataForm.shop_address"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商铺邮箱" prop="email">
          <el-input v-model="dataForm.email" auto-complete="off"></el-input>
        </el-form-item>

        <el-row>
          <el-form-item label="店铺图片" prop="business_license">
            <el-upload
              :action="imgUpload"
              list-type="picture-card"
              v-model="dataForm.pic"
              accept="image/gif, image/jpeg, image/jpg, image/png, image/svg"
              :limit="3"
              style="float:left"
              ref="shop_upload"
              :file-list="shopImgList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="法人身份证正面" prop="business_license">
            <el-upload
              :action="imgUpload"
              list-type="picture-card"
              v-model="dataForm.id_card_z"
              accept="image/gif, image/jpeg, image/jpg, image/png, image/svg"
              :limit="3"
              style="float:left"
              ref="id_card_z_upload"
              :file-list="idCardZImgList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="法人身份证反面" prop="business_license">
            <el-upload
              :action="imgUpload"
              list-type="picture-card"
              v-model="dataForm.id_card_f"
              accept="image/gif, image/jpeg, image/jpg, image/png, image/svg"
              :limit="3"
              style="float:left"
              ref="id_card_f_upload"
              :file-list="idCardFImgList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="营业执照" prop="business_license">
            <el-upload
              :action="imgUpload"
              list-type="picture-card"
              v-model="dataForm.business_license"
              accept="image/gif, image/jpeg, image/jpg, image/png, image/svg"
              :limit="3"
              style="float:left"
              ref="business_license_upload"
              :file-list="businessLicenseImgList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="食品安全证明" prop="business_license">
            <el-upload
              :action="imgUpload"
              list-type="picture-card"
              v-model="dataForm.food_safety"
              accept="image/gif, image/jpeg, image/jpg, image/png, image/svg"
              :limit="3"
              style="float:left"
              ref="food_safety_upload"
              :file-list="foodSafetyImgList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisible = false">{{
          $t("action.cancel")
        }}</el-button>
      </div>
    </el-dialog>

    <!--新增编辑界面-->
    <el-dialog
      title="营业时间配置"
      width="25%"
      :visible.sync="dialogVisibleOpenTime"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataFormOpenTime"
        :inline="true"
        label-width="80px"
        :rules="dataFormOpenTimeRules"
        ref="dataFormOpenTime"
        :size="size"
        label-position="right"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input
            v-model="dataFormOpenTime.id"
            :disabled="true"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="dataFormOpenTime.status">
            <el-option
              v-for="item in status"
              :key="item.key"
              :label="item.value"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisibleOpenTime = false">{{
          $t("action.cancel")
        }}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click="submitFormOpentime"
          :loading="editLoading"
          >{{ $t("action.submit") }}</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import utils from "@/assets/js/utils.js";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      imgUpload: this.utils.getUpLoadHostForImg(),
      size: "small",
      list: [],
      shoplist: [],
      shopImgList: [],
      idCardFImgList: [],
      idCardZImgList: [],
      businessLicenseImgList: [],
      foodSafetyImgList: [],
      status: [
        { key: "0", value: "审核通过" },
        { key: "1", value: "审核拒绝" }
      ],
      shopstatus: [{ key: "0", value: "启用" }, { key: "1", value: "禁用" }],
      type: [{ key: "0", value: "自营" }, { key: "1", value: "私营" }],
      bail_flag: [{ key: "Y", value: "是" }, { key: "N", value: "否" }],
      filters: {
        shopname: "",
        businessnumber: "",
        shopstatus: "",
        t: "shopApprove"
      },
      columns: [],
      checked: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      dialogVisibleOpenTime: false, //营业时间是否显示
      editLoading: false,
      dataFormRules: {},
      dataFormOpenTimeRules: {
        open_start_time: [
          { required: true, message: "请输入开始时间", trigger: "blur" }
        ],
        open_end_time: [
          { required: true, message: "请输入结束时间", trigger: "blur" }
        ]
      },
      // 新增编辑界面数据
      dataForm: {},
      // 营业时间设置
      dataFormOpenTime: {
        opentime: "",
        longitude: "",
        latitude: "",
        longitude: "",
        open_start_time: "",
        open_end_time: ""
      }
    };
  },

  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },

    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "店铺审核";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "businessnumber", label: "商家编号", minWidth: 120 },
        { prop: "businessaccounts", label: "商家账号", minWidth: 120 },
        { prop: "businessname", label: "商家名称", minWidth: 120 },
        { prop: "shopname", label: "店铺名称", minWidth: 150 },
        { prop: "signtime", label: "签约时间", minWidth: 120 },
        { prop: "expiretime", label: "到期时间", minWidth: 120 },
        { prop: "status", label: "审核状态", minWidth: 120 },
        { prop: "accountstatus", label: "账号状态", minWidth: 120 },
        { prop: "shopstatus", label: "店铺状态", minWidth: 120 }
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.findPage();
    },

    // 门店营业时间设置
    //选择框
    selectionChange: function(params) {
      this.checked = params.selections;
    },

    setStaus() {
      if (this.checked.length == 1) {
        this.dialogVisibleOpenTime = true;
        this.dataFormOpenTime.id = this.checked[0].id;
        this.dataFormOpenTime.status = this.checked[0].status;
      } else {
        this.$message({ message: "状态审核只能选择一条数据", type: "error" });
      }
    },
    submitFormOpentime() {
      this.$refs.dataFormOpenTime.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataFormOpenTime);
            params.t = "shopApprove";
            this.utils.request.editUserInfo(params, this.editInfoOpenTimeBack);
          });
        }
      });
    },
    // 新增修改回调
    editInfoOpenTimeBack: function(data) {
      if (data.code == "0000") {
        this.$message({ message: "操作成功", type: "success" });
        this.dialogVisibleOpenTime = false;
        this.editLoading = false;
      } else {
        this.$message({ message: "操作失败, " + data.msg, type: "error" });
      }
      this.findPage({ t: "shopApprove" });
    },

    //详情
    handleDetail(data) {
      this.shopImgList = [];
      this.idCardFImgList = [];
      this.idCardZImgList = [];
      this.businessLicenseImgList = [];
      this.foodSafetyImgList = [];
      let this_ = this;
      this.dialogVisible = true;
      let param = {};
      param.t = "shop";
      param.id = data.row.shop_id;
      this.utils.request.queryUserInfo(param, function(data) {
        if (data.data != null) {
          this_.dataForm = data.data;
          let shop = data.data.pic.split(","); //店铺图片
          let shopImg = {};
          for (let i = 0; i < shop.length; i++) {
            if (shop[i] != "") {
              shopImg.name = i;
              shopImg.url = shop[i];
              this_.shopImgList.push(shopImg);
              break;
            }
          }

          let idCardZ = data.data.id_card_z.split(","); //身份证正面图片
          let idCardZImg = {};
          for (let i = 0; i < idCardZ.length; i++) {
            if (idCardZ[i] != "") {
              idCardZImg.name = i;
              idCardZImg.url = idCardZ[i];
              this_.idCardZImgList.push(idCardZImg);
              break;
            }
          }

          let idCardF = data.data.id_card_f.split(","); //身份证反面图片
          let idCardFImg = {};
          for (let i = 0; i < idCardF.length; i++) {
            if (idCardF[i] != "") {
              idCardFImg.name = i;
              idCardFImg.url = idCardF[i];
              this_.idCardFImgList.push(idCardFImg);
              break;
            }
          }

          let foodSafety = data.data.food_safety.split(","); //食品安全照片
          let foodSafetyImg = {};
          for (let i = 0; i < foodSafety.length; i++) {
            if (foodSafety[i] != "") {
              foodSafetyImg.name = i;
              foodSafetyImg.url = foodSafety[i];
              this_.foodSafetyImgList.push(foodSafetyImg);
              break;
            }
          }

          let businessLicense = data.data.business_license.split(","); //食品安全照片
          let businessLicenseImg = {};
          for (let i = 0; i < businessLicense.length; i++) {
            if (businessLicense[i] != "") {
              businessLicenseImg.name = i;
              businessLicenseImg.url = businessLicense[i];
              this_.businessLicenseImgList.push(businessLicenseImg);
              break;
            }
          }
        } else {
          this_.dataForm = {};
        }
      });
    }
  },
  mounted() {
    this.initColumns();
  }
};
</script>

<style scoped>
/deep/ .el-input__inner   {
  width: 200px !important;
}
</style>